﻿<?php
/**
 * User: hyliu
 * Time: 17:46
 * 同步图片检测样例，调用会实时返回检测结果
 */

include_once 'aliyuncs/aliyun-php-sdk-core/Config.php';
use Green\Request\V20170112 as Green;
date_default_timezone_set("PRC");

$ak = parse_ini_file("aliyun.ak.ini");
//请替换成你自己的accessKeyId、accessKeySecret
$iClientProfile = DefaultProfile::getProfile("cn-hangzhou", $ak["accessKeyId"], $ak["accessKeySecret"]); // TODO
DefaultProfile::addEndpoint("cn-hangzhou", "cn-hangzhou", "Green", "green.cn-hangzhou.aliyuncs.com");
$client = new DefaultAcsClient($iClientProfile);

$request = new Green\ImageSyncScanRequest();
$request->setMethod("POST");
$request->setAcceptFormat("JSON");
$url = $_GET['urls'];
//$url = 'http://oscmpgu1a.bkt.clouddn.com/o_1bjrul60t1o4g1n6egb4sse1icl9.jpg';
$task1 = array('dataId' =>  uniqid(),
    'url' => $url,
    'time' => round(microtime(true)*1000)
);
$task2 = array('dataId' =>  uniqid(),
    'url' =>  'http://api.opencom.cn/ibuger_service/comm/comm_img.jsp?id=66172378&w=800&h=800',
    'time' => round(microtime(true)*1000)
);

/*$urls = ltrim($urls,',');
$urls = explode(',',$urls);
$tasks = array();
foreach ($urls as $key=>$url) {
    $tasks[$key]['url'] = $url;
    $tasks[$key]['time'] = round(microtime(true)*1000);
    $tasks[$key]['dataId'] = uniqid();
}*/

$request->setContent(json_encode(array("tasks" => $task1,
    "scenes" => array("porn"))));
$data = array();
try {
    $response = $client->getAcsResponse($request);
    if(200 == $response->code){
        $taskResults = $response->data;
        foreach ($taskResults as $key => $taskResult) {
            $data[$key]['url'] = $taskResult->url;
            if(200 == $taskResult->code){
                $sceneResults = $taskResult->results;
                foreach ($sceneResults as $sceneResult) {
                    $data[$key]['scene'] = $sceneResult->scene;
                    if ($sceneResult->suggestion == 'pass') {
                        $data[$key]['suggestion'] = '图片正常';
                    } else if($sceneResult->suggestion == 'review') {
                        $data[$key]['suggestion'] = '需要人工审核';
                    } else if ($sceneResult->suggestion == 'block') {
                        $data[$key]['suggestion'] = '图片违规，可以直接删除或者做限制处理';
                    }
                    $data[$key]['rate'] = $sceneResult->rate;//结果为该分类的概率；值越高，越趋于该分类
                    if ($sceneResult->label == 'normal') {
                        $data[$key]['label'] = '正常图片，无色情';
                    } else if($sceneResult->label == 'sexy'){
                        $data[$key]['label'] = '性感图片';
                    }else if($sceneResult->label == 'porn'){
                        $data[$key]['label'] = '色情图片';
                    }

                }
            }else{
              //  print_r("task process fail:" + $response->code);
            }
        }
        echo json_encode($data);exit;
    }else{
        //print_r("detect not success. code:" + $response->code);
    }
} catch (Exception $e) {
    echo json_encode($e);exit;
}



